package com.lzh.chatnote.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.lzh.chatnote.user.dao.UserDao;
import com.lzh.chatnote.user.dao.UserFriendDao;
import com.lzh.chatnote.user.domain.entity.User;
import com.lzh.chatnote.user.domain.entity.UserFriend;
import com.lzh.chatnote.common.domian.vo.request.CursorPageBaseReq;
import com.lzh.chatnote.common.domian.vo.response.CursorPageBaseResp;
import com.lzh.chatnote.user.domain.vo.response.user.UserFriendResp;
import com.lzh.chatnote.user.service.UserFriendService;
import com.lzh.chatnote.user.service.adapter.UserFriendAdapter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class UserFriendServiceImpl implements UserFriendService {
    @Resource
    private UserFriendDao userFriendDao;
    @Resource
    private UserDao userDao;

    @Override
    public CursorPageBaseResp<UserFriendResp> queryCursorPage(CursorPageBaseReq req) {
        CursorPageBaseResp<UserFriend> userFriendResp = userFriendDao.queryCursorPage(req);
        if (CollUtil.isEmpty(userFriendResp.getList())){
            return CursorPageBaseResp.buildEmpty();
        }

        List<UserFriend> userFriends = userFriendResp.getList();
        Set<Long> ids = userFriends.stream().map(UserFriend::getFriendId).collect(Collectors.toSet());
        List<User> userList = userDao.getUserByIds(ids);
        return CursorPageBaseResp.buildVO(userFriendResp, UserFriendAdapter.buildUserFriend(userFriends,userList));
    }
}
